package showtime.framework.algorithm.collections
{
	/**
	 * PriorityQueue 
	 * @author yinhao
	 * 
	 */	
	public class PriorityQueue
	{
		private var items:Array = null;
		
		public var sortMethod:Function = null;
		
		public function PriorityQueue()
		{
			items = [];
		}
		
		public function getNextItem():*
		{
			return items.shift();
	
		}
		
		public function hasNextItem():Boolean
		{
			return items.length > 0;
		}
		
		public function enqueue(item:*):void
		{
	
			var added:Boolean = false;
			
			for (var i:uint = 0, length:uint = items.length; i < length; ++i)
			{
				var currentItem:* = items[i];
				
		
				if (sortMethod(item, currentItem))
				{
					items.splice(i, 0, item);
					
					added = true;
					
					break;
				}
			}
			
			if (!added)
			{
				items.push(item);
			}
		}
		
		public function clear():void
		{
			items = [];
		}
		/**
		 * Default sort method. 
		 * @param obj1
		 * @param obj2
		 * @return 
		 * 
		 */		
		private function defaultSortMethod(obj1:*, obj2:*):Boolean
		{
			return obj1 < obj2;
		}
	}
}